package cat.joronya.supermanager.service.substrImpl;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Locale;

import cat.joronya.supermanager.market.Player;
import cat.joronya.supermanager.service.MarketParser;

public class MarketParserImpl implements MarketParser 
{
	public ArrayList<Player> players;
	
	// start position
	private static final String START_POSITION_STRING = "gif/fondomercado3arriba.gif";
	private static final String STOP_POSITION_STRING = "gif/fondomercado3abajo.gif";
	
	// start/end tr
	private static final String START_TR = "<tr>";
	private static final String STOP_TR = "</tr>";
	
	// lesion td
	private static final String START_LESION = "gif/lesion.gif";
	// baja td
	private static final String START_BAJA = "gif/baja.gif";
	// extra td
	private static final String START_EXTRA = "gif/extra.gif";
	// nacional td
	private static final String START_NACIONAL = "gif/nacional.gif";
	
	// start code player
	private static final String START_CODE_PLAYER = "<a href=\"http://www.acb.com/stspartidojug.php?cod_jugador=";
	private static final String STOP_CODE_PLAYER = "\" target";
	
	// start player name
	private static final String START_PLAYER_NAME = "<font color=\"black\">";
	private static final String STOP_PLAYER_NAME = "</font>";
	
	// start player info
	private static final String START_PLAYER_INFO = "onMouseover=\"ddrivetip('";
	private static final String STOP_PLAYER_INFO = "')\"; onMouseout";
	
	// start player team
	private static final String START_PLAYER_TEAM = "<td class=\"gris\">";
	private static final String STOP_PLAYER_TEAM = "</td>";
	
	// start player team balance
	private static final String START_BALANCE = "<td class=\"gris\" align=\"right\">";
	private static final String STOP_BALANCE = "</td>";
	
	// start player promedio
	private static final String START_PROMEDIO = "<td class=\"gris\" align=\"right\">";
	private static final String STOP_PROMEDIO = "<img src";
	
	// start player precio
	private static final String START_PRECIO = "<td class=\"grisdcha\" align=\"right\">";
	private static final String STOP_PRECIO = "<img src";
	
	//start player media minutos
	private static final String START_MEDIA_MINUTOS = "<td class=\"grisoscuro\" align=\"center\">";
	private static final String STOP_MEDIA_MINUTOS = "<img src";
	
	// start player val last day
	private static final String START_VAL_LD = "<td class=\"grisoscuro\" align=\"center\">";
	private static final String STOP_VAL_LD = "<img src";
	
	// start player val 3dp
	private static final String START_VAL_3DP = "<td class=\"grisoscuro\" align=\"center\">";
	private static final String STOP_VAL_3DP = "<img src";
	
	// start player +15%
	private static final String START_15_UP = "<td class=\"grisoscuroi\" align=\"center\">";
	private static final String STOP_15_UP = "<img src";
	
	// start player mantiene
	private static final String START_MANT = "<td class=\"grisoscuro\" align=\"center\">";
	private static final String STOP_MANT = "<img src";
	
	// start player -15%
	private static final String START_15_DOWN = "<td class=\"grisoscurod\" align=\"center\">";
	private static final String STOP_15_DOWN = "<img src";
	
	public long parse(String html, Integer jornada, String position) throws Exception 
	{
		long start = System.nanoTime();
		
		NumberFormat nf = NumberFormat.getInstance(new Locale("es","ES"));
		
		players = new ArrayList<Player>();
		
		int startPosition = html.indexOf(START_POSITION_STRING);
		if( startPosition == -1 )
			throw new Exception();
		
		int stopPosition = html.indexOf(STOP_POSITION_STRING);
		
		// recorrem els tr, comencem pel 3er tr
		int startTr, stopTr;
		startTr = html.indexOf(START_TR, startPosition);
		startTr = html.indexOf(START_TR, startTr+START_TR.length());
		startTr = html.indexOf(START_TR, startTr+START_TR.length());
		while( startTr != -1 && startTr < stopPosition )
		{
			// recuperem tr
			stopTr = html.indexOf(STOP_TR, startTr);
			String tr = html.substring(startTr, stopTr);
			
			// cerquem dades al tr
			Player p = new Player();
			
			// setejem la jornada
			p.setDay(jornada);
			
			// lesionat?
			if( tr.indexOf(START_LESION) != -1 )
				p.setStatus("lesio.png");
			
			// baja?
			if( tr.indexOf(START_BAJA) != -1 )
				p.setStatus("baixa.png");
			
			// nacional?
			if( tr.indexOf(START_NACIONAL) != -1 )
				p.setNacionality("nacional.png");
			
			// extra?
			if( tr.indexOf(START_EXTRA) != -1 )
				p.setNacionality("extra.png");
			
			// code player
			int startCode = tr.indexOf(START_CODE_PLAYER);
			int stopCode = tr.indexOf(STOP_CODE_PLAYER, startCode);
			String cod = tr.substring(startCode+START_CODE_PLAYER.length(), stopCode);
			p.setCodi(cod);
			
			// name player
			int startName = tr.indexOf(START_PLAYER_NAME, stopCode);
			int stopName = tr.indexOf(STOP_PLAYER_NAME, startName);
			String name = tr.substring(startName+START_PLAYER_NAME.length(), stopName); 
			p.setName(name);
			
			// inflo player
			int startInfo = tr.indexOf(START_PLAYER_INFO, stopName);
			int stopInfo = tr.indexOf(STOP_PLAYER_INFO, startInfo);
			int stopTeam;
			if( startInfo != -1 )
			{
				String info = tr.substring(startInfo+START_PLAYER_INFO.length(), stopInfo);
				p.setInfo(info);
				
				// team player
				int startTeam = tr.indexOf(START_PLAYER_TEAM, stopInfo);
				stopTeam = tr.indexOf(STOP_PLAYER_TEAM, startTeam);
				String team = tr.substring(startTeam+START_PLAYER_TEAM.length(), stopTeam);
				p.setTeam(team);
			}
			else
			{
				// team player
				int startTeam = tr.indexOf(START_PLAYER_TEAM, stopName);
				
				// cal saltar linia buida de info
				startTeam = tr.indexOf(START_PLAYER_TEAM, startTeam+START_PLAYER_TEAM.length());
				stopTeam = tr.indexOf(STOP_PLAYER_TEAM, startTeam);
				String team = tr.substring(startTeam+START_PLAYER_TEAM.length(), stopTeam);
				p.setTeam(team);
			}
			
			// team balance
			int startBalance = tr.indexOf(START_BALANCE, stopTeam);
			int stopBalance = tr.indexOf(STOP_BALANCE, startBalance);
			String balance = tr.substring(startBalance+START_BALANCE.length(), stopBalance);
			p.setBalance(balance);
			
			// player average
			int startPromedio = tr.indexOf(START_PROMEDIO, stopBalance);
			int stopPromedio = tr.indexOf(STOP_PROMEDIO, startPromedio);
			String promedio = tr.substring(startPromedio+START_PROMEDIO.length(), stopPromedio);
			Float average;
			try {
				average = Float.valueOf(nf.parse(promedio.trim()).floatValue());
			} catch (ParseException pe) {
				average = Float.valueOf("0");
			}
			p.setAverage(average);
			
			// player price
			int startPrice = tr.indexOf(START_PRECIO, stopPromedio);
			int stopPrice = tr.indexOf(STOP_PRECIO, startPrice);
			String precio = tr.substring(startPrice+START_PRECIO.length(), stopPrice);
			Integer price;
			try {
				price = Integer.valueOf(nf.parse(precio.trim()).intValue());
			} catch (ParseException pe) {
				price = Integer.valueOf(0);
			}
			p.setPrice(price);
			
			// minute
			int startMinutes = tr.indexOf(START_MEDIA_MINUTOS, stopPrice);
			int stopMinutes = tr.indexOf(STOP_MEDIA_MINUTOS, startMinutes);
			String minutes = tr.substring(startMinutes+START_MEDIA_MINUTOS.length(), stopMinutes);
			p.setMinutes(minutes);
			
			// rating
			int startValLd = tr.indexOf(START_VAL_LD, stopMinutes);
			int stopValLd = tr.indexOf(STOP_VAL_LD, startValLd);
			String valld = tr.substring(startValLd+START_VAL_LD.length(), stopValLd);
			Float rating;
			try {
				rating = Float.valueOf(nf.parse(valld.trim()).floatValue());
			} catch (ParseException pe) {
				rating = Float.valueOf("0");
			}
			p.setRating(rating);
			
			// rating 3dp
			int startVal3dp = tr.indexOf(START_VAL_3DP, stopValLd);
			int stopVal3dp = tr.indexOf(STOP_VAL_3DP, startVal3dp);
			String val3dp = tr.substring(startVal3dp+START_VAL_3DP.length(), stopVal3dp);
			Float ratingLast3Games;
			try {
				ratingLast3Games = Float.valueOf(nf.parse(val3dp.trim()).floatValue());
			} catch (ParseException pe) {
				ratingLast3Games = Float.valueOf("0");
			}
			p.setRatingLast3Games(ratingLast3Games);
			
			// +15%
			int start15up = tr.indexOf(START_15_UP, stopVal3dp);
			int stop15up = tr.indexOf(STOP_15_UP, start15up);
			String val15up = tr.substring(start15up+START_15_UP.length(), stop15up);
			Float increase15;
			try {
				increase15 = Float.valueOf(nf.parse(val15up.trim()).floatValue());
			} catch (ParseException pe) {
				increase15 = Float.valueOf("0");
			}
			p.setIncrease15(increase15);
			
			// mantiene
			int startMant = tr.indexOf(START_MANT, stop15up);
			int stopMant = tr.indexOf(STOP_MANT, startMant);
			String valMant = tr.substring(startMant+START_MANT.length(), stopMant);
			Float keepPrice;
			try {
				keepPrice = Float.valueOf(nf.parse(valMant.trim()).floatValue());
			} catch (ParseException pe) {
				keepPrice = Float.valueOf("0");
			}
			p.setKeepPrice(keepPrice);
			
			// -15%
			int start15Down = tr.indexOf(START_15_DOWN, stopMant);
			int stop15Down = tr.indexOf(STOP_15_DOWN, start15Down);
			String val15down = tr.substring(start15Down+START_15_DOWN.length(), stop15Down);
			Float decrease15;
			try {
				decrease15 = Float.valueOf(nf.parse(val15down.trim()).floatValue());
			} catch (ParseException pe) {
				decrease15 = Float.valueOf("0");
			}
			p.setDecrease15(decrease15);
			
			p.setPosition(position);
			players.add(p);
			
			// recuperem seguent startTr
			startTr = html.indexOf(START_TR, stopTr);
		}
		
		return System.nanoTime()-start;
	}
	
	public ArrayList<Player> getPlayers()
	{
		return players;
	}
}
